﻿Imports System.Data.SqlClient

Public Class frmInsuranceCompanies

    Sub FillInsuranceCompanies()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From InsuranceCompanies", cnn)
            Dim Reader As SqlDataReader

            Me.GridInsuranceCompanies.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.GridInsuranceCompanies.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("CompanyName"), Reader.Item("Tele1"), _
                                                   Reader.Item("Email"), Reader.Item("Address"), Reader.Item("Abbreviation"), _
                                                   "Update", "Delete"})
            End While
            cnn.Close()

            Me.lblResult.Text = "Count: " & Me.GridInsuranceCompanies.Rows.Count

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmIncurClassesList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillInsuranceCompanies()
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub GridReasons_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridInsuranceCompanies.CellClick
        If e.ColumnIndex = Me.DGColEdit.Index Then

            Dim a As New frmInsuranceCompaniesEdit
            a.SNo = Me.GridInsuranceCompanies.CurrentRow.Cells(0).Value
            a.ShowDialog()

            FillInsuranceCompanies()

        ElseIf e.ColumnIndex = Me.DGColDelete.Index Then
            If MsgBox("Confirm delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.WaitCursor

                    Dim cmd As New SqlCommand("Delete From InsuranceCompanies Where SNo=" & _
                                              Me.GridInsuranceCompanies.CurrentRow.Cells(0).Value, cnn)

                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    FillInsuranceCompanies()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub btnIns_Click(sender As System.Object, e As System.EventArgs) Handles btnIns.Click
        Dim a As New frmInsuranceCompaniesNew
        a.ShowDialog()

        FillInsuranceCompanies()
    End Sub
End Class